package 阿里巴巴机试题目;

import java.util.*;

public class 阿里巴巴2017年机试第2题 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int res;
        int _M_rows = 0;
        int _M_cols = 0;
        _M_rows = Integer.parseInt(in.nextLine().trim());
        _M_cols = Integer.parseInt(in.nextLine().trim());
        int[][] _M = new int[_M_rows][_M_cols];
        for (int _M_i = 0; _M_i < _M_rows; _M_i++) {
            String row_x = String.valueOf(in.nextLine().trim());
            String[] row_y = row_x.split(",");
            for (int _M_j = 0; _M_j < _M_cols; _M_j++) {
                _M[_M_i][_M_j] = Integer.parseInt(row_y[_M_j]);

            }
        }
        if (in.hasNextLine()) {
            in.nextLine();
        }
        res = findCircleNum(_M);
        System.out.println(String.valueOf(res));
    }

    private static int findCircleNum(int[][] m) {
        TreeMap<Integer, Integer> ts = new TreeMap<>();
        for (int[] m_row_x : m) {
            for (int j = 0; j < m_row_x.length; j++) {
                if (j != 0 && j != m_row_x.length - 1) {
                    int sum_x = getSum(m_row_x, 0, j);
                    if (!ts.containsKey(sum_x)) {
                        ts.put(sum_x, 1);
                    } else {
                        ts.put(sum_x, ts.get(sum_x) + 1);
                    }
                }
            }
        }
        System.out.println(ts.toString());

        int maxValue = Integer.MIN_VALUE;
        for (Map.Entry<Integer, Integer> x : ts.entrySet()) {
            maxValue = Math.max(x.getValue(), maxValue);
        }
        return m.length - maxValue;
    }

    private static int getSum(int[] m_row_x, int i, int j) {
        int sum = 0;
        for (int k = i; k <= j; k++) {
            sum += m_row_x[k];
        }
        return sum;
    }
}
